package ui

import applicationModel.Ataque
import dominio.JardinTablero
import dominio.Zombie
import home.HomeZombies
import org.uqbar.arena.bindings.NotNullObservable
import org.uqbar.arena.bindings.ObservableProperty
import org.uqbar.arena.bindings.PropertyAdapter
import org.uqbar.arena.layout.ColumnLayout
import org.uqbar.arena.widgets.Button
import org.uqbar.arena.widgets.Label
import org.uqbar.arena.widgets.Panel
import org.uqbar.arena.widgets.Selector
import org.uqbar.arena.windows.Dialog
import org.uqbar.arena.windows.WindowOwner
import org.uqbar.commons.utils.ApplicationContext
import org.eclipse.swt.SWT
import org.uqbar.commons.model.UserException

class AtacarWindow extends Dialog<Ataque> {

	@Property JardinTablero jt
	@Property Dialog<JardinTablero> own
	new(WindowOwner owner, Ataque ataque, JardinTablero jart) {
		super(owner, ataque)
		own = owner as Dialog<JardinTablero>
		title = "Ataca una fila!!"
		taskDescription = "Elige el Zombie que Atacara la Fila"
		jt = jart
	}

	// usamos una grilla aparte
	override protected createFormPanel(Panel mainPanel) {
		val editorPanel = new Panel(mainPanel)
		editorPanel.setLayout(new ColumnLayout(2))

		new Label(editorPanel).text = "Zombie Atacante"
		val selectorZombie = new Selector<Zombie>(editorPanel).allowNull(false)
		selectorZombie.bindValueToProperty("zombieAtacante")
		var propZombies = selectorZombie.bindItems(new ObservableProperty(homeZombies, "zombies"))
		propZombies.adapter = new PropertyAdapter(typeof(Zombie), "descripcionEntera")

	}

	override addActions(Panel actionsPanel) {
		val botonJugar = new Button(actionsPanel).setAsDefault.setCaption("Atacar")
		botonJugar.onClick[|this.atacar]
		botonJugar.bindEnabled(new NotNullObservable("zombieAtacante"))
		botonJugar.disableOnError

		val botonvolver = new Button(actionsPanel).setAsDefault.setCaption("Tablero")
		botonvolver.onClick[|cancel]

		val labelResultado = new Label(actionsPanel)
		labelResultado.setWidth(500)
		labelResultado.bindValueToProperty("resultado")

	}

	/*def bindContentsToProperty(Label label, String string) {
		throw new UnsupportedOperationException("TODO: auto-generated method stub")
	}*/
	def getHomeZombies() {
		ApplicationContext::instance.getSingleton(typeof(Zombie)) as HomeZombies
	}

	def atacar() {
		if (!modelObject.filaSeleccionada.filaNula) {
			try {
				modelObject.ataca()
				modelObject.resultado = modelObject.resultado //REFRESH??
				jt.refresh
			} catch (UserException e) {
				jt.refresh
				showError(e.getMessage)
				this.close
				own.close
			}
		} else {
			throw new UserException("Ingrese Plantas antes")
		}

	}

	/*override showError(String message) {
		this.showMessageBox(message, SWT.OK.bitwiseOr(SWT.ICON_ERROR))
	}*/

}
